normal_goods = {"F001": {"商品": "苹果", "单价": 1, "数量": 1},
                "F002": {"商品": "香蕉", "单价": 1, "数量": 1},
                "F003": {"商品": "雨伞", "单价": 5, "数量": 1},
                "F004": {"商品": "棒球", "单价": 2, "数量": 1},
                "F005": {"商品": "电池", "单价": 3, "数量": 1},
                "F006": {"商品": "水瓶", "单价": 4, "数量": 1}
                }
medicine_goods = {
    "M0001": {"name": "奥司他韦", "price": 60, "数量": 1},
    "M0002": {"name": "抗病毒口服液", "price": 30, "数量": 1}
}


class SanleShop:
    def __init__(self, goods, sys_info):
        self.goods = goods
        self.sys_info = sys_info
        self.userdict = {}

    def register(self, name, passwd, balance):
        if name not in self.userdict:
            user = USER(name, passwd, balance)
            self.userdict[name] = user
        else:
            print("用户已存在")

    def login(self, name, passwd):
        if name in system[c1].userdict and passwd == system[c1].userdict[name].passwd:
            print(f"登陆成功欢迎{name}")
        else:
            print("用户不存在或密码错误")

class USER:
    def __init__(self, name, passwd, balance):
        self.name = name
        self.passwd = passwd
        self.balance = balance
        self.shopping_cart = {}

    def shopping(self, total):
        if buy not in name.shopping_cart:
            name.shopping_cart = system[c1].goods[buy]
        else:
            name.shopping_cart[buy]["数量"] += 1
            for i in name.shopping_cart:
                print("购买了", name.shopping_cart[i]["商品"], f'{name.shopping_cart[i]["数量"]}个')
                total += name.shopping_cart[i]["单价"] * name.shopping_cart[i]["数量"]
            print(f'总价{total}价')
            name.shopping(total)
        if buy == 0:
            system[c1].userdict[name].balance = system[c1].userdict[name].balance - total
            print(system[c1].userdict[name].balance)


system1 = SanleShop(normal_goods, "普通购物系统")
system2 = SanleShop(medicine_goods, "药店购物系统")
system = [system1, system2]
USER("root", "123", 1000)
while 1:
    print("当前平台购物系统：")
    for i, j in enumerate(system):
        print(f"{i}.{j.sys_info}")
    c1 = input("输入：（q退出）")
    if c1 == "q":
        break
    if c1.isdigit() and int(c1) < len(system):
        c1 = int(c1)
        print(f"欢迎进入{system[c1].sys_info}".center(40, "="))
        c2 = input("1.登录\n2.注册\n输入选择（q退出）")
        if c2 == "q":
            break
        elif c2 == "1":
            name = input("用户名：")
            passwd = input("密码：")
            system[c1].login(name, passwd)
            print(f"{system[c1].sys_info}商品：{system[c1].goods}")
            buy = str(input("输入商品编号以添加至购物车,输入0以结算并退出："))
            total = 0
        elif c2 == "2":
            name, passwd, balance = input("请输入名字、密码、余额（空白分割）").split()
            system[c1].register(name, passwd, balance)
            print(f"当前用户有：{system[c1].userdict.keys()}")
        else:
            print("输入有误")
    else:
        print("输入错误")
